﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using IBupro.Classes;
using iBuproModel.Providers;
using iBuproModel.Repositories;
using iBuproModel.Repositories.Interfaces;
using IBupro.Controllers.Classes;
using IBupro.Models;

namespace IBupro.Controllers
{
    /// <summary>
    /// This controller handles Student business logic
    /// </summary>
    public class StudentController : CustomController
    {
        IUserRepository userRepository;
        iBuproModel.User student;



        public StudentController()
        {
            userRepository = repositoryService.UserRepositoryCreateInstance();
            
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult DiplomProject(string filter)
        {
            IDiplomProjectRepository diplomProjectRepository = repositoryService.DiplomProjectRepositoryCreateInstance();
            return View(diplomProjectRepository.GetAll().ToList());
        }


        [Authorize]
        public ActionResult Edit()
        {
            iBuproModel.User student = userRepository.GetUserByUserName(User.Identity.Name);
            return View(new StudentModel(student));
            
        }

        [Authorize]
        [HttpPost]
        public ActionResult Edit(StudentModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    IStudentFieldRepository studentFieldRepository = repositoryService.StudentFieldRepositoryCreateInstance();
                    
                    student = userRepository.GetUserByUserName(User.Identity.Name);

                    student.FirstName = model.FirstName;
                    student.LastName = model.LastName;
                    student.Email = model.Email;
                    student.StudentField = studentFieldRepository.First(f => f.Id == model.StudentField);

                    iBuproModel.Providers.IBuproMembershipProvider provider = new IBuproMembershipProvider();

                    if (!string.IsNullOrEmpty(model.OldPassword))
                    {
                        bool changePasswordStatus = false;
                        if (model.NewPassword == model.ConfirmPassword)
                        {
                            changePasswordStatus = provider.ChangePassword(student.Username, model.OldPassword, model.NewPassword);
                            userRepository.SaveChanges();
                            student = userRepository.GetUserByUserName(User.Identity.Name);
                            userRepository.SaveChanges();
                        }
                        if (changePasswordStatus == false)
                        {
                            model.ConfirmPassword = "";
                            model.NewPassword = "";
                            model.OldPassword = "";

                            FlashMessenger.AddMessage("Passwords doesn't match or bad old password!",MessageType.Error);
                            return View(model);
                        }
                    }

                    

                    userRepository.Update(student);

                    userRepository.SaveChanges();
                }

                return RedirectToAction("Index");
            }
            catch
            {
                return View(model);
            }
        }

        [ChildActionOnly]
        public ActionResult MyProjects()
        {
            student = userRepository.GetUserByUserName(User.Identity.Name);
            return View(student.Projects.ToList());
        }

        [ChildActionOnly]
        public ActionResult MyDiplomProjects()
        {
            student = userRepository.GetUserByUserName(User.Identity.Name);
            return View(student.StudentDiplomProjects.ToList());
        }

       

    }
}
